package cn.wolfcode.car.business.web.controller.system;


import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.query.CarPackageAuditQuery;
import cn.wolfcode.car.business.query.CarPackageAuditQuery2;
import cn.wolfcode.car.business.query.CarPackageAuditQueryObject;
import cn.wolfcode.car.business.service.ICarPackageAuditService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.base.query.QueryObject;
import cn.wolfcode.car.common.web.AjaxResult;
import cn.wolfcode.car.shiro.ShiroUtils;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 套餐审核列表控制器
 business.web.controller.system.CarPackageAuditController
 */
@Controller
@RequestMapping("system/carPackageAudit")
public class CarPackageAuditController {
    //模板前缀
    private static final String prefix = "business/system/carPackageAudit/";

    @Autowired
    private ICarPackageAuditService carPackageAuditService;
    @Autowired
    private IStatementService statementService;
    @Autowired
    private IUserService userService;


    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("system:carPackageAudit:view")
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }

    @RequiresPermissions("system:carPackageAudit:add")
    @RequestMapping("/addPage")
    public String addPage(){
        return prefix + "add";
    }


    @RequiresPermissions("system:carPackageAudit:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("carPackageAudit", carPackageAuditService.get(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("system:carPackageAudit:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> query(CarPackageAuditQueryObject qo){
        return carPackageAuditService.query(qo);
    }




    //删除
    @RequiresPermissions("system:carPackageAudit:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long id){
        carPackageAuditService.deleteBatch(id);
        return AjaxResult.success();
    }




       //saveItems保存操作
    @RequestMapping("/saveItems")
//    @RequiresPermissions("system:carPackageAudit:saveItems")
    @ResponseBody
    public AjaxResult saveItems(@RequestBody List<CarPackageAudit> carPackageAudits){
        return AjaxResult.success();
    }


    //processImg
    //流程图查看
    @RequestMapping("/processImg")
    public void processImg(Long id, HttpServletResponse response) throws IOException {
        InputStream inputStream=carPackageAuditService.getProcessImageByAuditId(id);
        IOUtils.copy(inputStream,response.getOutputStream());
    }
    //cancelApply
    //撤销
    @RequiresPermissions("system:carPackageAudit:cancelApply")
    @RequestMapping("/cancelApply")
    @ResponseBody
    public AjaxResult cancelApply(Long id){
       carPackageAuditService.cancelApply(id);
        return AjaxResult.success();
    }

    //我的待办
    //todoPage
//    @RequiresPermissions("system:carPackageAudit:todoPage")
    @RequestMapping("/todoPage")
    public String todoPage(){
        return prefix + "todoPage";
    }

//    @RequiresPermissions("business:carPackageAudit:todoQuery")
    @RequestMapping("/todoQuery")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> todoQuery(CarPackageAuditQuery qo){
        User user = ShiroUtils.getUser();
        //查询当前登录用户的待办
        qo.setAuditorId(user.getId());
        //查询正在审核中的待办
        qo.setStatus(CarPackageAudit.STATUS_IN_ROGRESS);
        return carPackageAuditService.queryTodo(qo);
    }

    //auditPage
    //审批处理跳转
    @RequestMapping("/auditPage")
    public String auditPage(Long id,Model model){
        model.addAttribute("id",id);
        return prefix + "audit";
    }

//audit审批处理
    @ResponseBody
    @RequestMapping("/audit")
    public AjaxResult audit(Long id ,Integer auditStatus,String info){
        carPackageAuditService.audit(id,auditStatus,info);
        return AjaxResult.success();
    }
    //我的已办数据获取跳转
    //doneQuery
    @RequestMapping("/doneQuery")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> doneQuery(CarPackageAuditQuery2 qo){
        User user = ShiroUtils.getUser();


        return carPackageAuditService.queryTodo2(qo);
    }
    //我的已办显示列表
    @RequestMapping("/donePage")
    public String donePage(){
        return prefix + "donePage";
    }


}
